/*
 * RTMLError.java is part of htn-core.
 *
 * RTMLError.java is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * RTMLError.java is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with RTMLError.java.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.htn.rtml;

import org.apache.log4j.Logger;
import org.htn.util.DateUtil;
import org.htn.util.XPathExtractor;
import org.htn.util.XmlTransformer;
import org.htn.util.XmlTransformerException;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.Namespace;

public class RTMLError {
	
	private static final Logger log = Logger.getLogger(RTMLError.class);
	
	private static final String template = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + 
			"<RTML mode=\"abort\" uid=\"rtml://yada\" version=\"3.1a\"\n" + 
			"	xmlns:crd=\"http://www.ivoa.net/xml/STC/STCcoords/v1.20\" xmlns=\"http://www.rtml.org/v3.1a\"\n" + 
			"	xmlns:reg=\"http://www.ivoa.net/xml/STC/STCregion/v1.20\" xmlns:stc=\"http://www.ivoa.net/xml/STC/stc-v1.20.xsd\"\n" + 
			"	xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n" + 
			"	xsi:schemaLocation=\"http://www.rtml.org/v3.1a http://monet.uni-sw.gwdg.de/XMLSchema/RTML/schemas/RTML-nightly.xsd\">\n" + 
			"	<History>\n" + 
			"	</History>\n" + 
			"</RTML>";

	private static final String historyXPath = "//RTML/History";
	
	private XmlTransformer xt = new XmlTransformer();
	private XPathExtractor xpe = new XPathExtractor();
	private DateUtil dateUtil = new DateUtil();
	
	private Namespace ns = Namespace.getNamespace("http://www.rtml.org/v3.1a");
	
	private String agentName;
	
	public RTMLError() {
		super();
	}
	
	public String buildError(Throwable t) {
		return buildError(RTMLError.template,t);
	}

	public String buildError(final String template, Throwable t) {
		String xml = null;
		try {
			Document doc = addEntry(buildDocument(template),t);
			xml = xt.toXmlString(doc);
		} catch (XmlTransformerException e) {
			try {
				Document doc = addEntry(buildDocument(RTMLError.template),e);
				xml = xt.toXmlString(doc);
			} catch (XmlTransformerException e1) {
				log.error(e);
			}
		}
		return xml;
	}
	
	private Document buildDocument(String template) throws XmlTransformerException {
		return xt.toDocument(template);
	}
	
	private Document addEntry(Document doc, Throwable t) {
		Element history = xpe.getElement(doc.getRootElement(), historyXPath);
		Element entry = new Element("Entry",ns)
		.setAttribute("timeStamp", dateUtil.getCurrentDateTimeNoZ())
		.addContent(
				new Element("Agent",ns)
				.setAttribute("name", this.agentName)
		)
		.addContent(
				new Element("Error",ns)
				.setText(t.getMessage())
		);
		history.addContent(entry);
		return doc;
	}
	
	public void setAgentName(String agentName) {
		this.agentName = agentName;
	}
	
}
